package fule.com.playandroidkotlin.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.WindowManager;

import java.util.List;

import fule.com.playandroidkotlin.base.BaseActivity;
import fule.com.playandroidkotlin.base.BasePresenter;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * 作者： ch
 * 时间： 2018/5/21 0021-下午 2:23
 * 描述：启动页
 * 来源：
 */


public class SplashActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
    private static final int RESULT_CODE = 1001;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if (msg.what == 1) {
                startA(MainActivity.class);
                finish();
            }
        }
    };

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void setStatusBar() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void initView() {

        requestPermision();
    }

    private void requestPermision() {
        String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Already have permission, do the thing
            isFirst();
            // ...
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "我们的app需要以下权限",
                    RESULT_CODE, perms);
        }
    }

    private void isFirst() {
        SharedPreferences preferences = context.getSharedPreferences("welcome", Context.MODE_PRIVATE);
        boolean isfirst = preferences.getBoolean("isfirst", true);

        if (isfirst) {
            startA(WelcomeActivity.class);
            preferences.edit().putBoolean("isfirst", false).commit();
            finish();
        } else {
            handler.sendEmptyMessageDelayed(1, 1500);
        }
    }

    @Override
    protected void addListener() {

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // Some permissions have been granted
        isFirst();
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        // Some permissions have been denied
        finish();
    }
}
